package com.example.config.datasource;


import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 这里是第一数据源 其他数据源配置同第二数据源配置
 */

@Configuration
@MapperScan(basePackages = "com.example.mapper.mysql", sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MysqlDataSourceConfig {
    @Bean("mysqlMybatisPlusConfig")
    @ConfigurationProperties(prefix = "mybatis-plus.configuration.mysql-log-config")
    public MybatisConfiguration globalConfiguration() {
        return new MybatisConfiguration();
    }
    @Bean(name = "mysqlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql-db")
    public DataSource starRocksDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "mysqlSqlSessionFactory")//配置工厂(配置数据扫描服务)
    public SqlSessionFactory sqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource,
                                               @Qualifier("mysqlMybatisPlusConfig") MybatisConfiguration config) throws Exception {

        MybatisSqlSessionFactoryBean mysqlSqlSessionFactory = new MybatisSqlSessionFactoryBean();
        mysqlSqlSessionFactory.setDataSource(dataSource);
        mysqlSqlSessionFactory.setConfiguration(config);

        mysqlSqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/mysql/*.xml"));
//        mysqlSqlSessionFactory.setMapperLocations(resolveMapperLocations());
        return mysqlSqlSessionFactory.getObject();
    }

    @Primary
    @Bean(name = "mysqlTransactionManager")//创建事务
    public DataSourceTransactionManager transactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Primary
    @Bean(name = "mysqlSqlSessionTemplate")//创建模板
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
